package com.kool.kmetrics.service;

import com.kool.kmetrics.consumer.TopicConstant;
import com.kool.kmetrics.metrics.*;
import com.kool.kmetrics.metrics.prometheus.NoAckSendPacketProm;
import com.kool.kmetrics.metrics.prometheus.ReceiveProm;
import com.kool.kmetrics.metrics.prometheus.SendProm;
import com.kool.kmetrics.metrics.prometheus.SessionProm;
import com.kool.kmetrics.common.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luyu
 */
@Service
@Slf4j
public class MetricsService {
    private Map<String, List<MetricsComputer>> metricsComputerMap;
    @Value("${prometheus_switch:false}")
    private boolean prometheusSwitch;

    @PostConstruct
    public void init() {
        metricsComputerMap = new HashMap<>();
        List<MetricsComputer> receiveComputers = new ArrayList<>();
        if (prometheusSwitch) {
            receiveComputers.add(new ReceiveComputer());
        }
        receiveComputers.add(SpringUtil.getBean(ReceiveProm.class));
        metricsComputerMap.put(TopicConstant.TOPIC_SUFFIX_RECEIVE_LOG, receiveComputers);

        List<MetricsComputer> sendComputers = new ArrayList<>();
        sendComputers.add(new SendComputer());
        if (prometheusSwitch) {
            sendComputers.add(SpringUtil.getBean(SendProm.class));
        }
        metricsComputerMap.put(TopicConstant.TOPIC_SUFFIX_SEND_LOG, sendComputers);

        List<MetricsComputer> sessionComputers = new ArrayList<>();
        sessionComputers.add(new SessionComputer());
        if (prometheusSwitch) {
            sessionComputers.add(SpringUtil.getBean(SessionProm.class));
        }
        metricsComputerMap.put(TopicConstant.TOPIC_SUFFIX_SESSION_LOG, sessionComputers);

        List<MetricsComputer> subscriptionComputers = new ArrayList<>();
        subscriptionComputers.add(SpringUtil.getBean(SubscriptionComputer.class));
        metricsComputerMap.put(TopicConstant.TOPIC_SUFFIX_SUBSCRIPTION_LOG, subscriptionComputers);

        List<MetricsComputer> noAckSendPacketComputers = new ArrayList<>();
        noAckSendPacketComputers.add(SpringUtil.getBean(NoAckSendPacketComputer.class));
        if (prometheusSwitch) {
            noAckSendPacketComputers.add(SpringUtil.getBean(NoAckSendPacketProm.class));
        }
        metricsComputerMap.put(TopicConstant.TOPIC_SUFFIX_NO_ACK_SEND_PACKET_LOG, noAckSendPacketComputers);

    }

    /**
     * 计算出站指标
     *
     * @param logType
     * @param log
     */
    public void dispatch(String logType, String log) {
        List<MetricsComputer> metricsComputers = metricsComputerMap.get(logType);
        if (metricsComputers != null) {
            for (MetricsComputer metricsComputer : metricsComputers) {
                metricsComputer.compute(log);
            }
        }

    }
}
